//
//  MyView.swift
//  BezierpathDemo
//
//  Created by hiking on 2019/7/25.
//  Copyright © 2019 hiking. All rights reserved.
//

import UIKit

class MyView: UIView {
    let redLine:Array<CGPoint> = [CGPoint.init(x: 0, y: 0),
                                  CGPoint.init(x: 30, y: 30),
                                  CGPoint.init(x: 60, y: 90),
                                  CGPoint.init(x: 90, y: 120),
                                  CGPoint.init(x: 120, y: 120),
                                  CGPoint.init(x: 150, y: 90),
                                  CGPoint.init(x: 180, y: 60),
                                  CGPoint.init(x: 210, y: 60),
                                  CGPoint.init(x: 240, y: 80),
                                  CGPoint.init(x: 270, y: 120),
                                  CGPoint.init(x: 300, y: 150),
                                  CGPoint.init(x: 300, y: 0),
                                  CGPoint.init(x: 0, y: 0)]
    
    let greenLine:Array<CGPoint> = [
        CGPoint.init(x: 0, y: 120),
        CGPoint.init(x: 30, y: 120),
        CGPoint.init(x: 60, y: 30),
        CGPoint.init(x: 90, y: 60),
        CGPoint.init(x: 120, y: 30),
        CGPoint.init(x: 150, y: 45),
        CGPoint.init(x: 180, y: 60),
        CGPoint.init(x: 210, y: 90),
        CGPoint.init(x: 240, y: 12),
        CGPoint.init(x: 270, y: 90),
        CGPoint.init(x: 300, y: 60),
        CGPoint.init(x: 300, y: 0),
        CGPoint.init(x: 0, y: 0)]
    
    let line:Array<CGPoint> = [ CGPoint.init(x: 0, y: 0),
                                    CGPoint.init(x: 30, y: 30),
                                    CGPoint.init(x: 60, y: 90),
                                    CGPoint.init(x: 90, y: 120),
                                    CGPoint.init(x: 120, y: 120),
                                    CGPoint.init(x: 150, y: 90),
                                  CGPoint.init(x: 180, y: 60),
                                    CGPoint.init(x: 210, y: 60),
                                  CGPoint.init(x: 240, y: 80),
                                  CGPoint.init(x: 270, y: 120),
                                  CGPoint.init(x: 300, y: 150),
                                    CGPoint.init(x: 300, y: 60),
                                    CGPoint.init(x: 270, y: 90),
                                    CGPoint.init(x: 240, y: 12),
                                    CGPoint.init(x: 210, y: 90),
                                    CGPoint.init(x: 180, y: 60),
                                    CGPoint.init(x: 150, y: 45),
                                    CGPoint.init(x: 120, y: 30),
                                    CGPoint.init(x: 90, y: 60),
                                    CGPoint.init(x: 60, y: 30),
                                    CGPoint.init(x: 30, y: 120),
                                    CGPoint.init(x: 0, y: 120),
                                 ]
    
   
    override func draw(_ rect: CGRect) {
        let redPath = getPath(points: redLine)
        let greenPath = getPath(points: greenLine)
        //绘制红绿区域
        draw(path:redPath,color: UIColor.red)
        draw(path:greenPath,color: UIColor.green)

        redPath.addClip()
        greenPath.addClip()

        draw(path: redPath, color: UIColor.gray)
        let con = UIGraphicsGetCurrentContext()
        con?.resetClip()
        drawLine()
    }
    func drawSuccess(redPath:UIBezierPath,greenPath:UIBezierPath)  {
        let con = UIGraphicsGetCurrentContext()
        
        con?.addPath(greenPath.cgPath)
        con?.addPath(redPath.cgPath)
        con?.setFillColor(UIColor.red.cgColor)
        con?.drawPath(using: CGPathDrawingMode.eoFill)
        
    }
    func drawRed() {
        let con = UIGraphicsGetCurrentContext()
        
        let redPath = UIBezierPath.init()
        redPath.move(to: redLine[0])
        for point in redLine {
            redPath.addLine(to: point)
        }



        let greenPath = UIBezierPath.init()
        greenPath.move(to: greenLine[0])
        for point in greenLine {
            greenPath.addLine(to: point)
        }

        redPath.addClip()
        greenPath.addClip()
        redPath.append(greenPath)

       

        let bluePath = UIBezierPath.init()
        bluePath.move(to: redLine[0])
        for point in redLine {
            bluePath.addLine(to: point)
        }

        bluePath.addClip()
        bluePath.append(redPath.reversing())

        con?.addPath(bluePath.cgPath)
        con?.setFillColor(UIColor.yellow.cgColor)
//        con?.setStrokeColor(UIColor.blue.cgColor)
        con?.drawPath(using: CGPathDrawingMode.fill)
        
    }
    func drawDemo()  {
        let con = UIGraphicsGetCurrentContext()
//        let successPath = UIBezierPath.init()
        
//        con?.saveGState()
//        con?.restoreGState()
//        let path2 = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: 50, height: 50))
//        path2.addClip()
        
         con?.saveGState()
        let path = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        path.addClip()
        let inpath = UIBezierPath.init(rect: CGRect.init(x: 30, y: 30, width: 50, height: 50))
        inpath.addClip()
        path.append(inpath)
       
        

        con?.restoreGState()
        let path2 = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        path2.addClip()
        
        path.append(path.reversing())

        con?.addPath(path2.cgPath)
        con?.setFillColor(UIColor.yellow.cgColor)
        con?.drawPath(using: CGPathDrawingMode.fill)
    }
    func draw(path:UIBezierPath,color:UIColor){
        let con = UIGraphicsGetCurrentContext()

        con?.addPath(path.cgPath)
        con?.setFillColor(color.cgColor)
//
        con?.drawPath(using: CGPathDrawingMode.fill)
//        con?.drawPath(using: CGPathDrawingMode.eoFill)
    }
    func getOninPath(path1:UIBezierPath,path2:UIBezierPath)->UIBezierPath{
        path1.append(path2)
        return path1
    }
    func getInPath(path1:UIBezierPath,path2:UIBezierPath) -> UIBezierPath {
        path1.addClip()
        path2.addClip()
        path1.append(path2)
        return path1
    }
    func getotherPath(path1:UIBezierPath,path2:UIBezierPath) -> UIBezierPath {
        path1.addClip()
        path1.append(path2.reversing())
        return path1
    }
    func drawLine(){
        let con = UIGraphicsGetCurrentContext()
        let path = UIBezierPath.init()
        path.move(to: CGPoint.init(x: 0, y: 75))
        path.addLine(to: CGPoint.init(x: 300, y: 75))
        path.lineWidth = 2
        con?.addPath(path.cgPath)
        con?.setFillColor(UIColor.black.cgColor)
        con?.drawPath(using: CGPathDrawingMode.stroke)
        
    }
    func getPath(points: Array<CGPoint>) -> UIBezierPath {
        let path = UIBezierPath.init()
        //设置起点
        path.move(to: points[0])
        for point in points {
            path.addLine(to: point)
        }

        return path
    }
    func drawArea(points:Array<CGPoint>,color:UIColor){
        let con = UIGraphicsGetCurrentContext()
        let path = UIBezierPath.init()
        //设置起点
        path.move(to: points[0])
        for point in points {
            path.addLine(to: point)
        }

        con?.addPath(path.cgPath)
        con?.setFillColor(color.cgColor)
        con?.drawPath(using: CGPathDrawingMode.fill)
    }
    func drawCircle() {
        let path = UIBezierPath.init(ovalIn: CGRect.init(x: 100, y: 100, width: 100, height: 100))
        UIColor.green.setStroke()
        path.addClip()//只有在path里才能看见，其他的切了。
        path.stroke()
        let path1 = UIBezierPath.init(ovalIn: CGRect.init(x: 100, y: 100, width: 80, height: 80))
        UIColor.green.setStroke()
        UIColor.red.setFill()
        //        path1.lineWidth = 10
        path1.stroke()
        path1.fill()
    }

    //补集
    func demo3(){
        let con = UIGraphicsGetCurrentContext()
        let path = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: 100, height: 100))
        path.addClip()
        let circlePath = UIBezierPath.init(ovalIn: CGRect.init(x: 60, y: 60, width: 80, height: 80))
//        let circleReversingPath = circlePath.reversing()
//        path.append(circleReversingPath)
        path.append(circlePath)
        con?.addPath(path.cgPath)
        con?.setFillColor(UIColor.yellow.cgColor)
        con?.drawPath(using: CGPathDrawingMode.fill)
    }
    //交集
    func demo2(){
        let con = UIGraphicsGetCurrentContext()
        let path = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        path.addClip()
        let circlePath = UIBezierPath.init(ovalIn: CGRect.init(x: 30, y: 30, width: 30, height: 30))
        circlePath.addClip()
//        path.append(circlePath)
        con?.addPath(path.cgPath)
        con?.setFillColor(UIColor.yellow.cgColor)
        con?.drawPath(using: CGPathDrawingMode.fill)
    }
    //并集
    func demo1() {
        let con = UIGraphicsGetCurrentContext()
        let path = UIBezierPath.init(rect: CGRect.init(x: 0, y: 0, width: 50, height: 50))
        let circlePath = UIBezierPath.init(ovalIn: CGRect.init(x: 30, y: 30, width: 30, height:30))
        path.append(circlePath)
        con?.addPath(path.cgPath)
        con?.setFillColor(UIColor.yellow.cgColor)
        con?.drawPath(using: CGPathDrawingMode.fill)
    }
   

}
